package com.example.zhangxuemin.recyclerview130zxm.model;

import com.example.zhangxuemin.recyclerview130zxm.bean.ArticleBean;
import com.example.zhangxuemin.recyclerview130zxm.iface.ArticleListener;
import com.example.zhangxuemin.recyclerview130zxm.iface.Articleiface;
import com.example.zhangxuemin.recyclerview130zxm.service.ArticleService;

import java.util.List;

import okhttp3.Cache;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zhangxuemin on 2017/5/16.
 */

public class Articlemodel implements Articleiface {
    private Retrofit retrofit;
    private static String BASE_URL="http://amicool.neusoft.edu.cn/";
    public Articlemodel() {
    retrofit =new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final ArticleListener listener) {
        ArticleService articleService=retrofit.create(ArticleService.class);
                Call<List<ArticleBean>> call=articleService.getResult(mod,page,sessionID);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    listener.onResponse(response.body());

                }
                else
                    listener.onFail("error");
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });




    }
}
